home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Texteditors / Origami / Sources / src / origami / filec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-27  |  4.4 KB  |  187 lines

  1. /*{{{}}}*/
  2. /*{{{  includes*/
  3. #ifdef CONFIG_H
  4. #   include "config.h"
  5. #endif
  6.  
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <dirent.h>
  10. #include <ctype.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <limits.h>
  14. #include <stdio.h>
  15. #include <pwd.h>
  16. #include <unistd.h>
  17.  
  18. #define FILEC_C
  19. #define I_GETTK_C
  20. #define I_MAIN_C
  21. #define I_MESSAGES_C
  22. #define I_SCREEN_C
  23.  
  24. #include "origami.h"
  25. #include <lib/ori_add_lib.h>
  26. /*}}}  */
  27.  
  28. #ifdef FILEC
  29.   /*{{{  user completion*/
  30.   private int user_completion(char *n)
  31.   {
  32.     boolean conflict;
  33.     boolean found;
  34.  
  35.     if (n[0]!=HOME_CHAR)
  36.        return(True);
  37.  
  38.     conflict=False;
  39.     found=False;
  40.     if (n[1])
  41.      /*{{{  try to expand*/
  42.      { char const *user;
  43.        int ul=strlen(n+1);
  44.  
  45.        start_getpwent();
  46.        for (;(user=next_getpwent());)
  47.           if (!strncmp(n+1,user,ul))
  48.            /*{{{  user matches the start*/
  49.              if (found)
  50.               /*{{{  get minimum extension*/
  51.               { char *old,c;
  52.                 char const *new;
  53.  
  54.                 for (old=n+1,new=user;;new++,old++)
  55.                    if ((c= *new)!=*old)
  56.                     { conflict=True;
  57.                       break;
  58.                     }
  59.                    else if (c=='\0')
  60.                       break;
  61.                  *old='\0';
  62.               }
  63.               /*}}}  */
  64.              else
  65.               /*{{{  use as completion*/
  66.               { strcpy(n+1,user);
  67.                 found=True;
  68.               }
  69.               /*}}}  */
  70.            /*}}}  */
  71.        end_getpwent();
  72.      }
  73.      /*}}}  */
  74.     return (conflict || !found);
  75.   }
  76.   /*}}}  */
  77. #endif
  78. /*{{{  filename completion*/
  79. char *filec(char *filen)
  80. {
  81.   char *ret=0;
  82. # ifdef FILEC
  83.   boolean user=False;
  84.  
  85.   if (*filen)
  86.    /*{{{  try to complete*/
  87.    { static char dir[_POSIX_PATH_MAX+1];
  88.      char full_path[_POSIX_PATH_MAX+_POSIX_PATH_MAX+1];
  89.      DIR *dirptr;
  90.      boolean conflict;
  91.  
  92.      /*{{{  maybe cut length*/
  93.      { int l;
  94.  
  95.        if ((l=strlen(filen))>=_POSIX_PATH_MAX)
  96.           filen+=_POSIX_PATH_MAX-l;
  97.      }
  98.      /*}}}  */
  99.      conflict=False;
  100.      /*{{{  get directory name*/
  101.      { if (dir_name((unsigned char*)dir,(unsigned char*)filen))
  102.         /*{{{  found path_separator, splitt filename*/
  103.         { filen+=strlen(dir);
  104.           home_expand(dir);
  105.         }
  106.         /*}}}  */
  107.        else
  108.         /*{{{  single filename*/
  109.         { if (filen[0]==HOME_CHAR)
  110.            /*{{{  expand user*/
  111.            { int l;
  112.  
  113.              strcpy(dir,filen);
  114.              l=strlen(dir);
  115.              if (!(conflict=user_completion(dir)))
  116.                 strcat(dir,PATH_SEP);
  117.              ret=dir+l;
  118.              user=True;
  119.            }
  120.            /*}}}  */
  121.           else
  122.            /*{{{  use . as dir*/
  123.              strcpy(dir,CURR_DIR);
  124.            /*}}}  */
  125.         }
  126.         /*}}}  */
  127.      }
  128.      /*}}}  */
  129.      if (!user)
  130.         if ((dirptr=opendir(dir)))
  131.          /*{{{  try to get file from directory*/
  132.          { struct dirent *f;
  133.            int fl;
  134.  
  135.            strcpy(full_path,dir);
  136.            for (fl=strlen(filen);(f=readdir(dirptr));)
  137.             /*{{{  check hit for file f*/
  138.               if (!strncmp(filen,f->d_name,fl))
  139.                /*{{{  file matches the start*/
  140.                  if (ret)
  141.                   /*{{{  get minimum completion*/
  142.                   { char *sf,*sr,c;
  143.  
  144.                     for (sr=ret,sf=f->d_name+fl;;sr++,sf++)
  145.                        if ((c= *sr)=='\0')
  146.                         { if (*sf)
  147.                              conflict=True;
  148.                           break;
  149.                         }
  150.                        else if (*sf!=c)
  151.                         { conflict=True;
  152.                           break;
  153.                         }
  154.                     *sr='\0';
  155.                   }
  156.                   /*}}}  */
  157.                  else
  158.                   /*{{{  use file as completion*/
  159.                   { strcpy(dir,f->d_name);
  160.                     ret=dir+fl;
  161.                   }
  162.                   /*}}}  */
  163.                /*}}}  */
  164.             /*}}}  */
  165.            closedir(dirptr);
  166.            if (!conflict && ret)
  167.             { strcat(full_path,PATH_SEP);
  168.               strcat(full_path,dir);
  169.               if (is_dir((FILE*)0,full_path))
  170.                  strcat(ret,PATH_SEP);
  171.             }
  172.          }
  173.          /*}}}  */
  174.         else
  175.            conflict=True;
  176.      if (!scr_off && verbose && (conflict || !ret || !*ret) )
  177.       { bell_audible();
  178.         bell_visible();
  179.       }
  180.    }
  181.    /*}}}  */
  182. # endif
  183.  
  184.   return(ret?ret:(char*)empty_text);
  185. }
  186. /*}}}  */
  187.